// pages/VideoLibrary/videoList/videolist.js
// pages/pm101/pm101.js
const App = getApp()
// let AJAX = require("../../../utils/util.js").AJAX
// let timeTranfer = require("../../../utils/util.js").timeTranfer
let { AJAX, timeTranfer, imgUrl } = require("../../../utils/util.js")
Page({

	/**
	 * 页面的初始数据
	 */
  data: {
    navH: '',    
    videoList: [],
    page: 1,
    total: 0,
     floorstatus: false,
    isRefreshing: false,
    isLoadingMoreData: false,
    hasNoData: false, 
    imgUrl: imgUrl
  },

	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    this.setData({
      navH: App.globalData.navHeight
    })
    
     if (!wx.getStorageSync("ACCESSTOKEN")) {
        App.userInfoReadyCallback = res => {
           wx.setStorageSync('ACCESSTOKEN', res);
           this.requestList()
        }
        return
     }
    this.requestList()
  },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },

	/**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {

  },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {

  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {
     this.setData({
        page: 1
     })
     this.requestList()
     // 显示顶部刷新图标
     wx.showNavigationBarLoading();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let vm = this   
    if (vm.data.total>0){
        vm.setData({
          isRefreshing: true
        })

      if (vm.data.videoList.length < vm.data.total) {
        vm.setData({
          page: vm.data.page + 1,
          isLoadingMoreData: true
        })
        console.log(this.data.page)
        vm.requestList(true)
      } else {
        this.setData({
          isLoadingMoreData: false
        })
      }
    }
   
   
  },
   // 获取滚动条当前位置
   onPageScroll: function (e) {
      console.log(e)
      if (e.scrollTop > 200) {
         this.setData({
            floorstatus: true
         });
      } else {
         this.setData({
            floorstatus: false
         });
      }
   },
   goTop: function (e) {  // 一键回到顶部
      if (wx.pageScrollTo) {
         wx.pageScrollTo({
            scrollTop: 0
         })
      } else {
         wx.showModal({
            title: '提示',
            content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
         })
      }
   }, 
  /**
   * 列表
   * idea/list
   */
  requestList: function (str) {
    let vm = this
    AJAX("video/index",'GET',{
      page: vm.data.page
    }).then(res => {    
       // 隐藏导航栏加载框
       wx.hideNavigationBarLoading();
       // 停止下拉动作
       wx.stopPullDownRefresh();  
      let _data = []     
      if (str) {
        _data = vm.data.videoList
      }
      if (res.data||res.data.length>0) {
        res.data.forEach(function (item, index) {
          item.created_at = timeTranfer(item.created_at,1)         
          _data.push(item)
        })
        vm.setData({
          videoList: _data,
          total: res.header["X-Pagination-Total-Count"],
          hasNoData: false
        })
      } else{
        this.setData({
          videoList: res.data,
          total: res.header["X-Pagination-Total-Count"],
          isLoadingMoreData: false,
          hasNoData: true
        })
      }
     
    
    })
  },
  jump(e){   
    let _id = e.currentTarget.id
    wx.navigateTo({
      url: '/pages/VideoLibrary/videoDetail/videoDetail?id=' + _id,
    })
  },
	
	/**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {

  },
  navBack() {
    wx.navigateBack()
  }
})